<template>
  <el-input
    v-bind="$attrs"
    v-model="state.paramVal"
    :type="type"
    clearable
    :placeholder="
      (typeof $attrs.disabled !== 'undefined' && $attrs.disabled !== false) ||
      state.formView
        ? ''
        : $attrs.placeholder
          ? $attrs.placeholder
          : `请输入${label}`
    "
    @keyup.enter.native="$emit('emitEnter')"
    :disabled="isValue($attrs.disabled) ? $attrs.disabled : state.formView"
    :title="state.paramVal"
    @blur="
      () =>
        (state.paramVal = state.paramVal ? String(state.paramVal).trim() : '')
    "
  >
    <template v-if="$slots.prefix" #prefix>
      <slot name="prefix" />
    </template>
    <template v-if="$slots.suffix" #suffix>
      <slot name="suffix" />
    </template>
    <template v-if="$slots.prepend" #prepend>
      <slot name="prepend" />
    </template>
    <template v-if="$slots.append" #append>
      <slot name="append" />
    </template>
  </el-input>
</template>

<script setup lang="ts">
import { reactive, watch, inject } from 'vue'
import { isValue } from '@/utils/czr-util'
const emit = defineEmits(['emitParam'])
const props = defineProps({
  param: {},
  label: {},
  type: {
    type: String,
    default: 'text',
    validator(val: string) {
      return ['text', 'textarea', 'password'].includes(val)
    },
  },
})
const state = reactive({
  paramVal: props.param,
  formView: inject('form-view', false),
})
watch(
  () => state.paramVal,
  (n) => {
    emit('emitParam', n)
  },
)
watch(
  () => props.param,
  (n) => {
    state.paramVal = n
  },
)
</script>

<style scoped lang="scss"></style>
